package com.xqiapp.utils.text;

import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class JsonUtil {
		
	public static String mapToJson(Object object) throws IOException
	{
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(object);
	}
	
	@SuppressWarnings("rawtypes")
	public static String ListToJson(List list) throws IOException
	{
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(list);
	}
	
	public static String objectToJson(Object object)throws IOException
	{
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(object);
	}
	
	@SuppressWarnings("unchecked")
	public static Map<String, Object> jsonToMap(String json)throws IOException
	{
		ObjectMapper mapper = new ObjectMapper();
		return mapper.readValue(json, Map.class);
	}
	
		public static void main(String args[]) throws Exception, IOException, IOException
		{
//			ObjectMapper mapper = new ObjectMapper();
	//		Map<String,Object> userData = mapper.readValue(new File("user.json"), Map.class);
		
			
		/*  将java对象装换成json数据*/
		/*	User u=new User();
			u.setAge("23");
			u.setName("xx");
			u.getHobby().add("pingbing");
			u.getHobby().add("dd");
			u.getHobby().add("e");
			u.getPartner().put("Name", "ss");
			u.getPartner().put("Name1", "ss");
			u.getPartner().put("Name2", "ss");
			System.out.println(mapper.writeValueAsString(u));*/
			
		/*	将json装换成pojo
			String s="{\"name\":\"xx\",\"age\":\"23\",\"hobby\":[\"pingbing\",\"dd\",\"e\"],\"partner\":{\"Name\":\"ss\",\"Name2\":\"ss\",\"Name1\":\"ss\"}}";
			User user=mapper.readValue(s, User.class);
			System.out.println(user.getAge()+user.getHobby().toString()+user.getPartner().toString());*/
		
		/*	map 转换成json
			Map<String,Object> map=new HashMap<String,Object>();
			Map<String,Object> map2=new HashMap<String,Object>();
			map2.put("currentPage", "3");
			map2.put("size", "10");
			map2.put("total", "10");
			map.put("name","d");
			map.put("pageInfo", map2);
			System.out.println(mapper.writeValueAsString(map));*/

			//json 装换成map
			String s="{\"pageInfo\":{\"total\":\"10\",\"currentPage\":\"3\",\"size\":\"10\"},\"name\":\"d\"}";
//			Map map=mapper.readValue(s,Map.class );
			Map<String, Object> map2=jsonToMap(s);
			System.out.println(map2);
			
//			Map<String,Object> map=new HashMap<String,Object>();
//			Map<String,Object> map2=new HashMap<String,Object>();
//			map2.put("currentPage", "3");
//			map2.put("size", "10");
//			map2.put("total", "10");
//			map.put("name","d");
//			List list=new ArrayList();
//			list.add(map2);
//			map.put("pageInfo", list);
			
//			System.out.println(mapper.writeValueAsString(map2));
//			Map<String,Object> map3 =  
			
		}
}
